route.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { NextRequest, NextResponse } from "next/server";
  2. async function handle(
  3. req: NextRequest,
  4. { params }: { params: { path: string[] } },
  5. ) {
  6. if (req.method === "OPTIONS") {
  7. return NextResponse.json({ body: "OK" }, { status: 200 });
  8. }
  9. const [protocol, ...subpath] = params.path;
  10. const targetUrl = `${protocol}://${subpath.join("/")}`;
  11. const method = req.headers.get("method") ?? undefined;
  12. const shouldNotHaveBody = ["get", "head"].includes(
  13. method?.toLowerCase() ?? "",
  14. );
  15. const fetchOptions: RequestInit = {
  16. headers: {
  17. authorization: req.headers.get("authorization") ?? "",
  18. },
  19. body: shouldNotHaveBody ? null : req.body,
  20. method,
  21. // @ts-ignore
  22. duplex: "half",
  23. };
  24. console.log("[Any Proxy]", targetUrl);
  25. const fetchResult = fetch(targetUrl, fetchOptions);
  26. return fetchResult;
  27. }
  28. export const GET = handle;
  29. export const POST = handle;
  30. export const PUT = handle;
  31. // nextjs dose not support those https methods, sucks
  32. export const PROFIND = handle;
  33. export const MKCOL = handle;
  34. export const runtime = "edge";